package org.jsola.hr.web;

import org.jsola.approval.vo.UserApproVO;
import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CostBillAddDTO;
import org.jsola.hr.dto.CostBillApproveDTO;
import org.jsola.hr.dto.CostBillConfirmDTO;
import org.jsola.hr.dto.CostBillUpdateDTO;
import org.jsola.hr.entity.CostBillDO;
import org.jsola.hr.query.CostBillQuery;
import org.jsola.hr.service.ICostBillService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 费用账单
 *
 * @author zhr
 */
@RestController("hrCostBillController")
@RequestMapping(HrConstants.API_PATH + "/v1/costBills")
public class CostBillController implements ICostBillControllerAPI {

    @Autowired
    private ICostBillService costBillService;

    @Override
    @PostMapping
    public Result<CostBillVO> save(@Valid @RequestBody CostBillAddDTO costBillAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增费用账单权限校验

        return Result.success(
                costBillService.save(costBillAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CostBillUpdateDTO costBillUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改费用账单权限校验

        int count = costBillService.update(costBillUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{costBillId}")
    public Result<Integer> delete(@PathVariable Long costBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除费用账单权限校验

        int count = costBillService.deleteByIds(tokenUser, costBillId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] costBillIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除费用账单权限校验

        int count = costBillService.deleteByIds(tokenUser, costBillIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{costBillId}")
    public Result<CostBillVO> findById(@PathVariable Long costBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看费用账单权限校验

        return Result.success(
                costBillService.selectById(costBillId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CostBillListVO>> listAll(@Valid CostBillQuery costBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看费用账单权限校验

        return Result.success(
                costBillService.select(costBillQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CostBillListVO>> page(@Valid CostBillQuery costBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                costBillService.selectPage(costBillQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/pagePost")
    public Result<Page<CostBillListVO>> pagePost(@RequestBody @Valid CostBillQuery costBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                costBillService.selectPage(costBillQuery, tokenUser)
        );
    }


    @Override
    @PostMapping(value = "/generatedBills")
    public Result<List<CostBillVO>> generatedBills(@Valid @RequestBody CostBillAddDTO costBillAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增费用账单权限校验

        return Result.success(
                costBillService.generatedBills(costBillAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectCostBillInfoById/{costBillId}")
    public Result<CostBillInfoGroupVO> selectCostBillInfoById(@PathVariable Long costBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看费用账单权限校验

        return Result.success(
                costBillService.selectCostBillInfoById(costBillId, tokenUser)
        );
    }

    @Override
    @PostMapping("/export/{costBillId}")
    public ResponseEntity<Resource> export(@PathVariable Long costBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 导出账单权限校验
        String excelFilePath = costBillService.exportCostBillInfoById(costBillId, tokenUser);
        // 编码文件名，防止乱码
        String filenames = FileKit.getNameAndEncode(excelFilePath);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @PostMapping(value = "/launchApproveByCompanyId")
    public Result<UserApproVO> launchApproveByCompanyId(@Valid @RequestBody CostBillApproveDTO costBillApproveDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 发起审批权限校验

        return Result.success(
                costBillService.launchApproveByCompanyId(costBillApproveDTO, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectByMonthSalaryId")
    public Result<CostBillVO> selectByMonthSalaryId(@RequestParam Long monthSalaryId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 发起审批权限校验
        CostBillDO costBillDO = costBillService.selectByMonthSalaryId(monthSalaryId, tokenUser);
        CostBillVO costBillVO = null;
        if (costBillDO != null) {
            costBillVO = costBillDO.to(CostBillVO.class);
        }
        return Result.success(costBillVO);
    }

    @Override
    @GetMapping("/selectCompanyByCostBillId")
    public Result<List<CompanyVO>> selectCompanyByCostBillId(@RequestParam Long costBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查询账单权限校验

        return Result.success(costBillService.selectCompanyByCostBillId(costBillId, tokenUser));
    }

    @Override
    @GetMapping("/selectByUserApprovalId")
    public Result<CostBillApprovalInfoVO> selectByUserApprovalId(@RequestParam String userApprovalId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查询账单权限校验

        return Result.success(
                costBillService.selectCostBillInfoByUserApprovalId(userApprovalId, tokenUser)
        );
    }

    @Override
    @PutMapping("/confirmCostBill")
    public Result<Integer> confirmCostBill(@Valid @RequestBody CostBillConfirmDTO costBillConfirmDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查询账单权限校验

        return Result.success(
                costBillService.confirmCostBill(costBillConfirmDTO, tokenUser)
        );
    }
}

